package com.example.hadoop.client;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.util.Iterator;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Brian
 * @Date: 2020/04/18/22:20
 * @Description:
 */
public class HadoopClientDemo {

    FileSystem fs = null;
    Configuration conf = null;
    private static final String USER = "root";

    @Before
    public void init() throws Exception {
        conf = new Configuration();
        fs = FileSystem.get(new URI("hdfs://shizhan:9000"), conf, USER);
    }

    @Test
    public void showConf() {
        Iterator<Map.Entry<String, String>> it = conf.iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> entry = it.next();
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
    }

    @Test
    public void testUpload() throws Exception {
        fs.copyFromLocalFile(new Path("d:/111_local.txt"), new Path("/"));
    }

    @Test
    public void testDownload() throws IOException {
        fs.copyToLocalFile(new Path("/111_local.txt"), new Path("d:/111.txt"));
    }

    @Test
    public void listFiles() throws IOException {
        RemoteIterator<LocatedFileStatus> fileStatus = fs.listFiles(new Path("/"), true);
        while (fileStatus.hasNext()) {
            LocatedFileStatus file = fileStatus.next();
            System.out.println("BlockSize: " + file.getBlockSize());
            System.out.println("Owner: " + file.getOwner());
            System.out.println("Replication: " + file.getReplication());
            System.out.println("ModifyTime: " + file.getModificationTime());
            System.out.println("Path: " + file.getPath());
            System.out.println("权限：" + file.getPermission());
            BlockLocation[] locations = file.getBlockLocations();
            for (BlockLocation location : locations) {
                System.out.println("偏移量：" + location.getOffset());
                System.out.println("块长度：" + location.getLength());
                String[] hosts = location.getHosts();
                for (String host: hosts) {
                    System.out.println("存放的主机：" + host);
                }
                String[] names = location.getNames();
                for (String name : names) {
                    System.out.println("块名称：" + name);
                }
            }
            System.out.println("----------------------------");
            System.out.println("----------------------------");
            System.out.println("----------------------------");
        }
    }

    @Test
    public void testMkdir() throws Exception {
        boolean mkdirs = fs.mkdirs(new Path("/aaa/bbb/ccc"));
        System.out.println(mkdirs);
    }

    @Test
    public void testDeleteDir() throws Exception {
        boolean delete = fs.delete(new Path("/"), true);
        System.out.println(delete);
    }

    @After
    public void afterRun() throws IOException {
         fs.close();
    }
}
